package com.mj.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mj.req.BaseResp;
import com.mj.user.entity.User;
import com.mj.user.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("user")
@Api(description = "用户的注册和登录")
public class UserController {

	@Autowired
	private UserService userService;
	
	@ApiOperation(value = "登录页面")
	@RequestMapping(value = "index", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView login() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("login");
		return mav;
	}

	@ApiOperation(value = "用户登录")
	@RequestMapping(value = "login", method = RequestMethod.GET)
	@ResponseBody
	public BaseResp login(@RequestParam String userName, @RequestParam String password) {
		BaseResp resq = new BaseResp<>();
		resq.setResultNote("登录失败");
		resq.setResult(0);
		if (StringUtils.isBlank(userName)) {
			resq.setResultNote("账号不能为空");
			return resq;
		}
		if (StringUtils.isBlank(password)) {
			resq.setResultNote("密码不能为空");
			return resq;
		}
		try {
			User user = userService.getByColumn(userName);
			if(user != null ){
				if (password.equals(user.getPassword())) {
					resq.setResultNote("登录成功");
					resq.setResult(1);
				} else {
					resq.setResultNote("密码错误");
				}
			}else {
				resq.setResultNote("用户不存在");
			}
		} catch (Exception e) {
			resq.setResultNote("登录失败");
			e.printStackTrace();
		}
		return resq;
	}

	@ApiOperation(value = "用户注册")
	@RequestMapping(value = "register", method = RequestMethod.POST)
	@ResponseBody
	public BaseResp register(@RequestBody User user) {
		BaseResp resq = new BaseResp<>();
		resq.setResult(0);
		if (StringUtils.isBlank(user.getEmail())) {
			resq.setResultNote("邮箱不能为空");
			return resq;
		}
		if (StringUtils.isBlank(user.getEmail())) {
			resq.setResultNote("用户名不能为空");
			return resq;
		}
		if (StringUtils.isBlank(user.getPassword())) {
			resq.setResultNote("密码不能为空");
			return resq;
		}
		try {
			User user2 = userService.getByColumn(user.getEmail());
			if (user2 != null) {
				resq.setResultNote("邮箱已存在");
			}else {
				userService.insert(user);
				resq.setResultNote("注册成功");
			}
		} catch (Exception e) {
			resq.setResultNote("注册失败");
			e.printStackTrace();
		}
		return resq;
	}

	@ApiOperation(value = "修改用户信息")
	@RequestMapping(value = "update", method = RequestMethod.PUT)
	@ResponseBody
	public BaseResp update(@RequestBody User user) {
		BaseResp resq = new BaseResp<>();
		try {
			userService.update(user);
			resq.setResultNote("更新成功");
		} catch (Exception e) {
			resq.setResultNote("更新失败");
			e.printStackTrace();
		}
		return resq;
	}
}
